package com.exam.controller;

import com.exam.entity.Admin;
import com.exam.entity.AdminLoginParam;
import com.exam.service.IAdminService;
import com.exam.util.JwtTokenUtil;
import com.exam.util.RespBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录控制器
 *
 * @author zhoubin
 * @since 1.0.0
 */
@Api(tags = "LoginController")
@RestController
@Slf4j
public class LoginController {
    @Autowired
    private IAdminService adminService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public RespBean login(@RequestBody AdminLoginParam adminLoginParam, HttpServletRequest request) {
        log.info("AdminLoginParam:{}",adminLoginParam);
        return adminService.login(adminLoginParam.getAdminId1(),adminLoginParam.getPwd(),adminLoginParam.getCode(),request);
    }

    @ApiOperation(value = "获取当前用户信息")
    @GetMapping("/admin/info")
    public Admin getAdminInfo(Principal principal){
        if (null==principal){
            return null;
        }
        String name = principal.getName();
        log.info("adminId:{}",name);
        Admin admin=adminService.getAdminByUserName(name);
        admin.setPwd(null);
        return admin;
    }


    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public RespBean logout(){
        return RespBean.success("注销成功！");
    }

}